package com.bcu.wish.controller;

import com.bcu.wish.config.GlobalConfig;
import com.bcu.wish.entity.Message;
import com.bcu.wish.service.TimeService;
import com.bcu.wish.service.UserinfoService;
import com.bcu.wish.utils.LoadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

@Api(tags = "用户信息模块")
@RestController
@RequestMapping("/api/userinfo")
public class UserinfoController {

    @Resource
    private UserinfoService userinfoService;

    @Resource
    private TimeService timeService;

    @Resource
    private GlobalConfig globalConfig;

    @ApiOperation(value = "根据userId查询userinfo")
    @GetMapping(value = "/info")
    public Message getUserinfo(String userId) {
        Date begindate = timeService.selectByPrimaryKey(5).getObj().getTimeLimittime(); //开始查询成绩时间
        Date stopdate = timeService.selectByPrimaryKey(6).getObj().getTimeLimittime();  //截止查询成绩时间
        Date nowdate = new Date();
        if (nowdate.after(begindate)) {
            if (nowdate.before(stopdate)) {
                return userinfoService.selectByPrimaryKey(userId);
            }
            return Message.fail("查询成绩时间已截止！");
        }
        return Message.fail("查询成绩时间未开始！");
    }

    @ApiOperation(value = "查询所有学生成绩")
    @GetMapping(value = "/all")
    public Message getAllUserinfo() {
        return userinfoService.selectAll();
    }

    @ApiOperation(value = "上传userinfoExcel更新数据库")
    @PostMapping(value = "/load",headers = "content-type=multipart/form-data")
    public Message getImages(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request) throws IOException {
        String fileName =LoadUtil.upload(globalConfig.filePath,files, request);
        //String UploadLink = globalConfig.hostUrl+"resource/"+ fileName;

        return userinfoService.getExcel(fileName);

    }


}
